package com.gaff.emp.core.modules.auth.controller;

import cn.hutool.core.date.DateUtil;
import com.gaff.emp.core.EmpException;
import com.gaff.emp.core.R;
import com.gaff.emp.core.modules.auth.LoginUserInfo;
import com.gaff.emp.core.modules.auth.annotation.Auth;
import com.gaff.emp.core.modules.auth.annotation.LoginUser;
import com.gaff.emp.core.util.ContextUtil;
import com.gaff.emp.core.util.Identities;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
public class TestController {

    public static final List<UploadLog> logs = new ArrayList<>();

    @GetMapping("/test")
    public R test() {
        return R.ok();
    }

    @GetMapping("/authTest")
    @Auth
    public R authTest() {
        return R.ok();
    }

    @GetMapping("/authTestPerm")
    @Auth(perm = "role")
    public R authPermTest() {
        return R.ok();
    }

    @GetMapping("/loginUserInfoTest")
    @Auth(perm = "role")
    public R loginUserInfoTest(@LoginUser LoginUserInfo userInfo) {
        log.info("aa" + userInfo);
        return R.ok();
    }

    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        log.info(file.getName());
        String fileName = Identities.uuid();
        String dir = ContextUtil.getStaticPath() + "upload" + File.separator;
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        file.transferTo(new File(dir + fileName));
        UploadLog log = new UploadLog();
        log.setFileName(fileName);
        log.setUploadTime(DateUtil.now());
        log.setStatus(0);
        logs.add(0, log);
        return R.ok(fileName);
    }

    @GetMapping("/getUploadList")
    public R getUploadList() {
        int start = logs.size() - 10;
        if (start < 0) {
            start = 0;
        }
        return R.ok(logs.subList(start, logs.size()));
    }

    @GetMapping("/status/{fileName}/{status}/{card}")
    public R status(@PathVariable("fileName") String fileName, @PathVariable("status") Integer status, @PathVariable("card") String card) {
        UploadLog log = null;
        for (UploadLog log1 : logs) {
            if (log1.getFileName().equals(fileName)) {
                log = log1;
                break;
            }
        }
        if (log == null) {
            throw new EmpException(("error"));
        }
        log.setStatus(status);
        log.setCard(card);
        log.setTime(DateUtil.now());
        return R.ok();
    }

    @GetMapping("/queryLog/{fileName}")
    public R status(@PathVariable("fileName") String fileName) {
        for (UploadLog log : logs) {
            if (log.getFileName().equals(fileName)) {
                return R.ok(log);
            }
        }
        throw new EmpException(("error"));
    }

    @PostMapping("/uploadMultipleFiles")
    public R uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
        for (MultipartFile file : files) {
            log.info(file.getName());
        }
        return R.ok();
    }

    @GetMapping("/download")
    public void downloadFile() throws IOException {
        byte[] bytes = new byte[1024];
        ContextUtil.download(MediaType.APPLICATION_XML.toString(), bytes);
    }

    @Data
    public static class UploadLog {

        private String fileName;

        private String uploadTime;

        // 0: 新上传，1：停好，2：未停好
        private int status;

        private String time;

        private String card;

    }

}
